/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tletlis.model;
import java.awt.Color;
import java.util.*;
import tletlis.model.Area.BrickEvent;

/**
 * The Abstract brick class. The super-class to all of the bricks.
 * @author Gustav
 */
public abstract class AbstractBrick {


    protected Color color;
    
    protected int distance;
    
    List<Position> cords;

  /**
   * Abstract method intended to return the coordinats of a brick
   * after it has been rotated.
   * @return List<Position>
   */
    protected abstract List<Position> rotateBrick();

 /**
  * Method returning the color of the brick
  * @return Color
  */
    public Color getColor() {
        return color;
    }

 /**
  * Method returning the coordinates of the brick
  * @return List<Position>
  */
    protected List<Position> getCords() {
        return cords;
    }

 /**
  * Method returning the coordinates of a brick after it has been moved.
  * The method handles, both vertical and horizontal movements via if-statements.
  * @return List<Position>
  */
    public List<Position> moveBrick(BrickEvent dir) {
        List<Position> temp = new ArrayList<Position>();
        for(Position p: cords) {
            temp.add(new Position(p.getY(),p.getX()));
        }
        if(dir == BrickEvent.RIGHT) {
            
            for(int i = 0; i < cords.size(); i++) {
                temp.set(i, new Position((cords.get(i).getY()),(cords.get(i).getX()+distance)));
                
            }
        }
        if(dir == BrickEvent.LEFT) {
            for(int i = 0; i < cords.size(); i++) {
                temp.set(i, new Position((cords.get(i).getY()),(cords.get(i).getX()-distance)));
            }
        } 
        if(dir == BrickEvent.DOWN) {
            for(int i = 0; i < cords.size(); i++) {
                temp.set(i, new Position((cords.get(i).getY()+distance),(cords.get(i).getX())));
            }
        }
        if(dir == BrickEvent.ROTATE) {
            temp = rotateBrick();
        }
            
        return temp;
    }

 /**
  * Method handling setting the coordinates of the brick.
  * Throws IndexOutOfBoundsException the parameter has to many coordinates.
  * @param pos The positions supposed to be set.
  */
    public synchronized void  setBrick(List<Position> pos, boolean rotate) {
        if(rotate) {
            nextState();
        }
        if(pos.size() != cords.size()) {
            throw new IndexOutOfBoundsException(); //TODO nån annan exception?
        }
        else {
            cords = pos;
        }
        
    }

    protected abstract void nextState();

 /**
  * Method returning the top left corner of the brick.
  * @return int
  */
    public int getTopX() {
        int topX = cords.get(0).getX();
        for(int i = 1; i < cords.size(); i++) {
            int temp =cords.get(i).getX();
            if(temp < topX)
                topX = temp;
        }
        topX /= Position.getLength();
        return topX;
    }
}
